package com.kkd.kkdapp.view.shapeimageview;

import com.kkd.kkdapp.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;


public class PorterShapeImageView extends PorterImageView {
	private Drawable shape;
	private Matrix matrix;
	private Matrix drawMatrix;

	public PorterShapeImageView(Context context) {
		super(context);
		setup(context, null, 0);
	}

	public PorterShapeImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setup(context, attrs, 0);
	}

	public PorterShapeImageView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		setup(context, attrs, defStyle);
	}

	private void setup(Context context, AttributeSet attrs, int defStyle) {
		if (attrs != null) {
			TypedArray typedArray = context.obtainStyledAttributes(attrs,
					R.styleable.ShaderImageView, defStyle, 0);
			shape = typedArray.getDrawable(R.styleable.ShaderImageView_siShape);
			typedArray.recycle();
		}
		matrix = new Matrix();
	}

	@Override
	protected void paintMaskCanvas(Canvas maskCanvas, Paint maskPaint,
			int width, int height) {
		if (shape != null) {
			if (shape instanceof BitmapDrawable) {
				configureBitmapBounds(width, height);
				if (drawMatrix != null) {
					int drawableSaveCount = maskCanvas.getSaveCount();
					maskCanvas.save();
					maskCanvas.concat(matrix);
					shape.draw(maskCanvas);
					maskCanvas.restoreToCount(drawableSaveCount);
					return;
				}
			}

			shape.setBounds(0, 0, width, height);
			shape.draw(maskCanvas);
		}
	}

	private void configureBitmapBounds(int viewWidth, int viewHeight) {
		drawMatrix = null;
		int drawableWidth = shape.getIntrinsicWidth();
		int drawableHeight = shape.getIntrinsicHeight();
		boolean fits = viewWidth == drawableWidth
				&& viewHeight == drawableHeight;

		if (drawableWidth > 0 && drawableHeight > 0 && !fits) {
			shape.setBounds(0, 0, drawableWidth, drawableHeight);
			float widthRatio = (float) viewWidth / (float) drawableWidth;
			float heightRatio = (float) viewHeight / (float) drawableHeight;
			float scale = Math.min(widthRatio, heightRatio);
			float dx = (int) ((viewWidth - drawableWidth * scale) * 0.5f + 0.5f);
			float dy = (int) ((viewHeight - drawableHeight * scale) * 0.5f + 0.5f);

			matrix.setScale(scale, scale);
			matrix.postTranslate(dx, dy);
		}
	}
		
	Bitmap colorBmp;
	public void setImageColor(int nColor) {
		if(colorBmp==null){
			colorBmp = Bitmap.createBitmap(4, 4, Config.RGB_565);
		}
		Canvas canvas = new Canvas(colorBmp);
		canvas.drawColor(nColor);
		setImageBitmap(colorBmp);

	}
	
	@Override
	public void recycle() {
		// TODO Auto-generated method stub
		super.recycle();
		colorBmp.recycle();
	}
}